
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>StatefulWidget · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter5/" />
    
    
    <link rel="prev" href="section1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    Flutter vs React Native
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    环境配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.2.1" data-path="../chapter2/section1.html">
            
                <a href="../chapter2/section1.html">
            
                    
                    前置依赖环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2.2" data-path="../chapter2/section2.html">
            
                <a href="../chapter2/section2.html">
            
                    
                    配置 Android 环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2.3" data-path="../chapter2/section3.html">
            
                <a href="../chapter2/section3.html">
            
                    
                    配置 Flutter 环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2.4" data-path="../chapter2/section4.html">
            
                <a href="../chapter2/section4.html">
            
                    
                    创建并运行 Flutter 项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    核心Widget的使用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="../chapter3/section3.html">
            
                <a href="../chapter3/section3.html">
            
                    
                    MaterialApp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="../chapter3/section1.html">
            
                <a href="../chapter3/section1.html">
            
                    
                    Text
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3" data-path="../chapter3/section2.html">
            
                <a href="../chapter3/section2.html">
            
                    
                    Center
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.4" data-path="../chapter3/section4.html">
            
                <a href="../chapter3/section4.html">
            
                    
                    Scaffold
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.5" data-path="../chapter3/section5.html">
            
                <a href="../chapter3/section5.html">
            
                    
                    常用的布局组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.6" data-path="../chapter3/section6.html">
            
                <a href="../chapter3/section6.html">
            
                    
                    AppBar
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.7" data-path="../chapter3/section7.html">
            
                <a href="../chapter3/section7.html">
            
                    
                    TabBar
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.8" data-path="../chapter3/section8.html">
            
                <a href="../chapter3/section8.html">
            
                    
                    TabController
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.9" data-path="../chapter3/section9.html">
            
                <a href="../chapter3/section9.html">
            
                    
                    TabBarView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.10" data-path="../chapter3/section10.html">
            
                <a href="../chapter3/section10.html">
            
                    
                    Drawer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.11" data-path="../chapter3/section11.html">
            
                <a href="../chapter3/section11.html">
            
                    
                    UserAccountsDrawerHeader
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.12" data-path="../chapter3/section12.html">
            
                <a href="../chapter3/section12.html">
            
                    
                    ListView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.13" data-path="../chapter3/section13.html">
            
                <a href="../chapter3/section13.html">
            
                    
                    CircleAvatar
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="./">
            
                <a href="./">
            
                    
                    自定义 Widget
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.4.1" data-path="section1.html">
            
                <a href="section1.html">
            
                    
                    StatelessWidget
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.1.4.2" data-path="section2.html">
            
                <a href="section2.html">
            
                    
                    StatefulWidget
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    事件绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    dio 数据请求
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.7" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    路由导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8" data-path="../chapter8/">
            
                <a href="../chapter8/">
            
                    
                    修改 App 名称和图标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.9" data-path="../chapter9/">
            
                <a href="../chapter9/">
            
                    
                    打包发布 Android 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.10" data-path="../chapter10/">
            
                <a href="../chapter10/">
            
                    
                    项目中用到的资源
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >StatefulWidget</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="statefulwidget">StatefulWidget</h1>
<p>StatefulWidget &#x662F;&#x6709;&#x72B6;&#x6001;&#x63A7;&#x4EF6;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x63A7;&#x4EF6;&#x62E5;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x79C1;&#x6709;&#x6570;&#x636E;&#x548C;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#xFF0C;&#x57FA;&#x672C;&#x5B9A;&#x4E49;&#x8FC7;&#x7A0B;&#x5982;&#x4E0B;&#xFF1A;</p>
<h2 id="&#x5B9A;&#x4E49;&#x6709;&#x72B6;&#x6001;&#x7684;&#x63A7;&#x4EF6;">&#x5B9A;&#x4E49;&#x6709;&#x72B6;&#x6001;&#x7684;&#x63A7;&#x4EF6;</h2>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A; &#x7535;&#x5F71;&#x8BE6;&#x60C5; &#x63A7;&#x4EF6;&#xFF0C;&#x7EE7;&#x627F;&#x81EA; StatefulWidget</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieDetail</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatefulWidget</span> </span>{
  <span class="hljs-comment">// &#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x5F53;&#x524D;&#x7EC4;&#x4EF6;&#x5FC5;&#x987B;&#x7684; id &#x5C5E;&#x6027;</span>
  MovieDetail({Key key, <span class="hljs-meta">@required</span> <span class="hljs-keyword">this</span>.id}) : <span class="hljs-keyword">super</span>(key: key);

  <span class="hljs-comment">// &#x7535;&#x5F71;&#x7684;Id&#x503C;</span>
  <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> id;

  <span class="hljs-comment">// StatefulWidget &#x63A7;&#x4EF6;&#x5FC5;&#x987B;&#x5B9E;&#x73B0; createState &#x51FD;&#x6570;</span>
  <span class="hljs-comment">// &#x5728; createState &#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x5FC5;&#x987B;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x7EE7;&#x627F;&#x81EA; State&lt;T&gt; &#x72B6;&#x6001;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;</span>
  <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x7684; _MovieDetailState &#x5C31;&#x7EE7;&#x627F;&#x81EA; State&lt;T&gt;</span>
  _MovieDetailState createState() =&gt; <span class="hljs-keyword">new</span> _MovieDetailState();
}
</code></pre>
<h2 id="&#x5B9A;&#x4E49;&#x7EE7;&#x627F;&#x81EA;-statet-&#x7684;&#x72B6;&#x6001;&#x7C7B;">&#x5B9A;&#x4E49;&#x7EE7;&#x627F;&#x81EA; <code>State&lt;T&gt;</code> &#x7684;&#x72B6;&#x6001;&#x7C7B;</h2>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x8FD9;&#x4E2A;&#x7EE7;&#x627F;&#x81EA; State&lt;T&gt; &#x7684;&#x7C7B;&#xFF0C;&#x4E13;&#x95E8;&#x7528;&#x6765;&#x5B9A;&#x4E49;&#x6709;&#x72B6;&#x6001;&#x63A7;&#x4EF6;&#x7684; &#x4E1A;&#x52A1;&#x903B;&#x8F91; &#x548C; &#x79C1;&#x6709;&#x6570;&#x636E;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_MovieDetailState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span>&lt;<span class="hljs-title">MovieDetail</span>&gt; </span>{
  <span class="hljs-comment">// build &#x51FD;&#x6570;&#x662F;&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x7528;&#x6765;&#x6E32;&#x67D3;&#x5F53;&#x524D;&#x6709;&#x72B6;&#x6001;&#x63A7;&#x4EF6;&#x5BF9;&#x5E94;&#x7684; UI &#x7ED3;&#x6784;</span>
  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-comment">// &#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x8FD9;&#x4E2A; _MovieDetailState &#x72B6;&#x6001;&#x7C7B;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; widget &#x5BF9;&#x8C61;&#x8BBF;&#x95EE;&#x5230; StatefulWidget &#x63A7;&#x4EF6;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5E76;&#x76F4;&#x63A5;&#x4F7F;&#x7528;</span>
    <span class="hljs-comment">// &#x4F8B;&#x5982;&#xFF1A;widget.id</span>
    <span class="hljs-keyword">return</span> Text(<span class="hljs-string">&apos;MovieDetail --&apos;</span> + widget.id);
  }
}
</code></pre>
<h2 id="&#x5B9A;&#x4E49;&#x548C;&#x4FEE;&#x6539;&#x79C1;&#x6709;&#x6570;&#x636E;">&#x5B9A;&#x4E49;&#x548C;&#x4FEE;&#x6539;&#x79C1;&#x6709;&#x6570;&#x636E;</h2>
<pre><code class="lang-dart">
<span class="hljs-comment">// &#x8FD9;&#x4E2A;&#x7EE7;&#x627F;&#x81EA; State&lt;T&gt; &#x7684;&#x7C7B;&#xFF0C;&#x4E13;&#x95E8;&#x7528;&#x6765;&#x5B9A;&#x4E49;&#x6709;&#x72B6;&#x6001;&#x63A7;&#x4EF6;&#x7684; &#x4E1A;&#x52A1;&#x903B;&#x8F91; &#x548C; &#x79C1;&#x6709;&#x6570;&#x636E;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_MovieDetailState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span>&lt;<span class="hljs-title">MovieDetail</span>&gt; </span>{
  <span class="hljs-comment">// 1. &#x5B9A;&#x4E49;&#x79C1;&#x6709;&#x72B6;&#x6001;&#x6570;&#x636E;&#x3010;&#x4EE5; _ &#x5F00;&#x5934;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x662F;&#x5F53;&#x524D;&#x7C7B;&#x7684;&#x79C1;&#x6709;&#x6570;&#x636E;&#x3011;</span>
  <span class="hljs-built_in">int</span> _count;

  <span class="hljs-comment">// 2. &#x901A;&#x8FC7; initState &#x751F;&#x547D;&#x5468;&#x671F;&#x51FD;&#x6570;&#xFF0C;&#x6765;&#x521D;&#x59CB;&#x5316;&#x79C1;&#x6709;&#x6570;&#x636E;</span>
  <span class="hljs-meta">@override</span>
  <span class="hljs-keyword">void</span> initState() {
    <span class="hljs-keyword">super</span>.initState();
    <span class="hljs-comment">// 2.1 &#x628A; _count &#x7684;&#x503C;&#x521D;&#x59CB;&#x5316;&#x4E3A; 0</span>
    _count = <span class="hljs-number">0</span>;
  }

  <span class="hljs-comment">// build &#x51FD;&#x6570;&#x662F;&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x7528;&#x6765;&#x6E32;&#x67D3;&#x5F53;&#x524D;&#x6709;&#x72B6;&#x6001;&#x63A7;&#x4EF6;&#x5BF9;&#x5E94;&#x7684; UI &#x7ED3;&#x6784;</span>
  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-comment">// &#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x8FD9;&#x4E2A; _MovieDetailState &#x72B6;&#x6001;&#x7C7B;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; widget &#x5BF9;&#x8C61;&#x8BBF;&#x95EE;&#x5230; StatefulWidget &#x63A7;&#x4EF6;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5E76;&#x76F4;&#x63A5;&#x4F7F;&#x7528;</span>
    <span class="hljs-comment">// &#x4F8B;&#x5982;&#xFF1A;widget.id</span>
    <span class="hljs-keyword">return</span> Column(
      children: &lt;Widget&gt;[
        Text(<span class="hljs-string">&apos;MovieDetail --&apos;</span> + widget.id + <span class="hljs-string">&apos; --- &apos;</span> + _count.toString()),
        RaisedButton(
          child: Icon(Icons.add),
          <span class="hljs-comment">// 3. &#x6307;&#x5B9A;&#x70B9;&#x51FB;&#x4E8B;&#x4EF6;&#x7684;&#x5904;&#x7406;&#x51FD;&#x6570;&#x4E3A; _add</span>
          onPressed: _add,
        )
      ],
    );
  }

  <span class="hljs-comment">// 4. &#x5B9A;&#x4E49; _count &#x81EA;&#x589E;&#x7684;&#x51FD;&#x6570;&#x3010;&#x4EE5; _ &#x5F00;&#x5934;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x662F;&#x79C1;&#x6709;&#x51FD;&#x6570;&#x3011;</span>
  <span class="hljs-keyword">void</span> _add() {
    <span class="hljs-comment">// &#x5982;&#x679C;&#x8981;&#x4E3A;&#x79C1;&#x6709;&#x6570;&#x636E;&#x91CD;&#x65B0;&#x8D4B;&#x503C;&#xFF0C;&#x5FC5;&#x987B;&#x8C03;&#x7528; setState() &#x51FD;&#x6570;</span>
    setState(() {
      <span class="hljs-comment">// &#x8BA9;&#x79C1;&#x6709;&#x6570;&#x636E; _count &#x81EA;&#x589E; +1</span>
      _count++;
    });
  }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="section1.html" class="navigation navigation-prev " aria-label="Previous page: StatelessWidget">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter5/" class="navigation navigation-next " aria-label="Next page: 事件绑定">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"StatefulWidget","level":"1.1.4.2","depth":3,"next":{"title":"事件绑定","level":"1.1.5","depth":2,"path":"chapter5/README.md","ref":"chapter5/README.md","articles":[]},"previous":{"title":"StatelessWidget","level":"1.1.4.1","depth":3,"path":"chapter4/section1.md","ref":"chapter4/section1.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter4/section2.md","mtime":"2018-12-15T10:49:06.673Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-12-17T10:25:33.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

